package scau.erps.view.base;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;

import scau.erps.app.Erps;
import scau.erps.entity.Material;
import scau.erps.service.MaterialService;
import scau.erps.service.impl.MaterialServiceImpl;
import scau.erps.util.CalculateMaterial;
import scau.erps.util.CalculateQuarter;

/** 
 * @author  outsider
 * @date 创建时间：2015年5月16日 下午6:45:45 
 * 
 * 用于申请原材料订单
 *   */
public class MaterialOrderDialog extends JDialog{
	private static final long serialVersionUID = 1L;
	private JTextField m1TextField;
	private JTextField m2TextField;
	private JTextField m3TextField;
	private JTextField m4TextField;
	private JTextField priceTextField;
	private JTextField periodM1M2TextField,periodM3M4TextField;
	private JPanel contentPanel;
	private MaterialService materialService=Erps.getIocInstance().get(MaterialServiceImpl.class);
	public MaterialOrderDialog() {
       this.setTitle("原材料订单");
       this.setSize(450, 330);
		contentPanel = new JPanel();
		contentPanel.setLayout(new BorderLayout());
		contentPanel.add(getMaterialOrderPanel(), BorderLayout.CENTER);
		this.setContentPane(contentPanel);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.setVisible(true);
       
	}


	public JPanel getMaterialOrderPanel(){
		JPanel materialOrderPanel = new JPanel();
		
		materialOrderPanel.setLayout(null);
		
		JLabel m1Label = new JLabel("M1");
		m1Label.setBounds(50, 35, 54, 20);
		materialOrderPanel.add(m1Label);
		
		JLabel m2Label = new JLabel("M2");
		m2Label.setBounds(50, 75, 54, 20);
		materialOrderPanel.add(m2Label);
		
		JLabel m3Label = new JLabel("M3");
		m3Label.setBounds(50, 115, 54, 20);
		materialOrderPanel.add(m3Label);
		
		JLabel m4Label = new JLabel("M4");
		m4Label.setBounds(50, 155, 54, 20);
		materialOrderPanel.add(m4Label);
		
		JLabel periodM1M2Label = new JLabel("M1M2账期");
		periodM1M2Label.setBounds(208, 75, 90, 20);
		materialOrderPanel.add(periodM1M2Label);
		
		JLabel periodM3M4Label = new JLabel("M3M4账期");
		periodM3M4Label.setBounds(208, 115, 90, 20);
		materialOrderPanel.add(periodM3M4Label);
		
		JLabel priceLabel = new JLabel("总价");
		priceLabel.setBounds(208, 155, 54, 20);
		materialOrderPanel.add(priceLabel);		
		
		m1TextField = new JTextField();
		m1TextField.setText("0");
		m1TextField.setBounds(114, 35, 66, 34);
		materialOrderPanel.add(m1TextField);
		m1TextField.setColumns(10);
		
		m2TextField = new JTextField();
		m2TextField.setText("0");
		m2TextField.setColumns(10);
		m2TextField.setBounds(114, 75, 66, 34);
		materialOrderPanel.add(m2TextField);
		
		m3TextField = new JTextField();
		m3TextField.setText("0");
		m3TextField.setColumns(10);
		m3TextField.setBounds(114, 115, 66, 34);
		materialOrderPanel.add(m3TextField);
		
		m4TextField = new JTextField();
		m4TextField.setText("0");
		m4TextField.setColumns(10);
		m4TextField.setBounds(114, 155, 66, 34);
		materialOrderPanel.add(m4TextField);
		
		priceTextField = new JTextField();
		priceTextField.setText("0");
		priceTextField.setEditable(false);
		priceTextField.setBounds(290, 155, 66, 34);
		materialOrderPanel.add(priceTextField);
		priceTextField.setColumns(10);
		
		periodM1M2TextField = new JTextField();
		periodM1M2TextField.setText("现金");
		periodM1M2TextField.setEditable(false);
		periodM1M2TextField.setColumns(10);
		periodM1M2TextField.setBounds(290, 75, 66, 34);
		materialOrderPanel.add(periodM1M2TextField);
		
		periodM3M4TextField = new JTextField();
		periodM3M4TextField.setText("现金");
		periodM3M4TextField.setEditable(false);
		periodM3M4TextField.setColumns(10);
		periodM3M4TextField.setBounds(290, 115, 66, 34);
		materialOrderPanel.add(periodM3M4TextField);
		
		//添加文本改变监听器
		Document[] doc=new Document[4];
		doc[0]=m1TextField.getDocument();
		doc[1]=m2TextField.getDocument();
		doc[2]=m3TextField.getDocument();
		doc[3]=m4TextField.getDocument();
		for (int i=0;i<=3;i++){
			doc[i].addDocumentListener(new DocumentListener() {
				public void changedUpdate(DocumentEvent arg0) {
					changePrice();
					changeperiodM1M2();
					changeperiodM3M4();
				}
				@Override
				public void insertUpdate(DocumentEvent arg0) {	
					changePrice();
					changeperiodM1M2();
					changeperiodM3M4();
				}
				public void removeUpdate(DocumentEvent arg0) {	
					changePrice();
					changeperiodM1M2();
					changeperiodM3M4();
				}
			});
		}

		
		JButton confirmButton = new JButton("确认订单");
		/**
		 * @author outsider
		 * 下原材料订单
		 * @see ZJC 增加了输入判断，不能输入字符或者小于0的数字；
		 */	    
		confirmButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			
				int m1=0,m2=0,m3=0,m4=0,tot=0; String message="";
				int team=Erps.getCurrentTeam().getId();
				String beginTime=Erps.getSand().getTime();
				String getTimeM1M2=CalculateQuarter.addOneQuarter(Erps.getSand().getTime());
				String getTimeM3M4=CalculateQuarter.addTwoQuarter(Erps.getSand().getTime());
				String payTimeM1M2=getTimeM1M2;
				String payTimeM3M4=getTimeM3M4;
				try{
				String periodM1M2=(CalculateMaterial.getperiod(m1TextField.getText(), m2TextField.getText()));
			    String periodM3M4=(CalculateMaterial.getperiod(m3TextField.getText(), m4TextField.getText()));
			    if (periodM1M2.equals("现金")==false) payTimeM1M2=CalculateQuarter.calculateQuarter(getTimeM1M2,periodM1M2);
			    if (periodM3M4.equals("现金")==false) payTimeM3M4=CalculateQuarter.calculateQuarter(getTimeM3M4,periodM3M4);
			    
				if (m1TextField.getText().equals("")==false) m1=Integer.parseInt(m1TextField.getText());
				if (m2TextField.getText().equals("")==false) m2=Integer.parseInt(m2TextField.getText());
				if (m3TextField.getText().equals("")==false) m3=Integer.parseInt(m3TextField.getText());
				if (m4TextField.getText().equals("")==false) m4=Integer.parseInt(m4TextField.getText());
				if(m1<0||m2<0||m3<0||m4<0)  throw new NumberFormatException();
	
				if (m1>0){
				   Material materialOrder=new Material(team, "M1", m1, beginTime, getTimeM1M2, payTimeM1M2, "已申请");
				   materialService.applyMaterialOrder(materialOrder);
				   tot++;
				   message=message+"M1数量： "+m1+" 入库时间 "+getTimeM1M2+" 付款时间 "+payTimeM1M2+"\n";
				}
				if (m2>0){
				   Material materialOrder=new Material(team, "M2", m2, beginTime, getTimeM1M2, payTimeM1M2, "已申请");
				   materialService.applyMaterialOrder(materialOrder);
				   tot++;
				   message=message+"M2数量： "+m2+" 入库时间 "+getTimeM1M2+" 付款时间 "+payTimeM1M2+"\n";
				}
				if (m3>0){
				    Material materialOrder=new Material(team, "M3", m3, beginTime, getTimeM3M4, payTimeM3M4, "已申请");
					materialService.applyMaterialOrder(materialOrder);
					tot++;
					message=message+"M3数量： "+m3+" 入库时间 "+getTimeM3M4+" 付款时间 "+payTimeM3M4+"\n";
				}
				if (m4>0){
				    Material materialOrder=new Material(team, "M4", m4, beginTime, getTimeM3M4, payTimeM3M4, "已申请");
					materialService.applyMaterialOrder(materialOrder);
					tot++;
					message=message+"M4数量： "+m4+" 入库时间 "+getTimeM3M4+" 付款时间 "+payTimeM3M4+"\n";
				}
				    message=message+"本季度共申请 "+tot+" 笔原材料订单";
				    JOptionPane.showMessageDialog(null, message, "提示",JOptionPane.INFORMATION_MESSAGE);
				    dispose();
			}
				catch(NumberFormatException e){
					JOptionPane.showMessageDialog(null, "请输入大于零的数字", "提示",JOptionPane.INFORMATION_MESSAGE);
				}
			}
		
         
		});
		confirmButton.setBounds(156, 215, 93, 23);
		materialOrderPanel.add(confirmButton);
		
		JButton skipButton = new JButton("本季度不购买");
		skipButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				dispose();		
			}
		});
		skipButton.setBounds(251, 215, 120, 23);
		materialOrderPanel.add(skipButton);
		return materialOrderPanel;
	}
	//监听总价文本框方法
	private void changePrice(){
		int m1=0,m2=0,m3=0,m4=0;
		if (m1TextField.getText().equals("")==false) m1=Integer.parseInt(m1TextField.getText());
		if (m2TextField.getText().equals("")==false) m2=Integer.parseInt(m2TextField.getText());
		if (m3TextField.getText().equals("")==false) m3=Integer.parseInt(m3TextField.getText());
		if (m4TextField.getText().equals("")==false) m4=Integer.parseInt(m4TextField.getText());
		priceTextField.setText(Integer.toString(m1+m2+m3+m4));
	}
	//监听M1M2账期框方法

	private void changeperiodM1M2(){	
		periodM1M2TextField.setText(CalculateMaterial.getperiod(m1TextField.getText(), m2TextField.getText()));
	}
	//监听M3M4账期框方法
	private void changeperiodM3M4(){
		periodM3M4TextField.setText(CalculateMaterial.getperiod(m3TextField.getText(), m4TextField.getText()));
	}
}
